package work.utils;

import static java.lang.System.out;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.h819.core.commons.Configurations;
import com.h819.core.db.MyDbUtils;

/**
 * @ClassName: StandardClassUtils
 * @Description: TODO(标准类别工具类. 应用的时候注意，因为每次都要查询数据库，故要一次查询完成所有的数据，不要频繁调用)
 * @author h819
 * @date June 10, 2009 16:15:24 PM
 * @version V0.1
 */
public class StandardClassUtils {

	private static StandardClassUtils instance = null;
	static Logger log = Logger.getLogger(StandardClassUtils.class);
	PropertiesConfiguration conf = Configurations
			.getInstance("work.properties");
	private Connection conn;
	private QueryRunner runner;

	/**
	 * 单例模式，禁止实例化
	 */
	private StandardClassUtils() {

		// init
		conn = MyDbUtils.getInstance("work.properties").getConnection(
				"oracle.jdbc.driver.OracleDriver",
				conf.getString("jdbc.oracle.work.235.url"),
				conf.getString("jdbc.oracle.work.235.user"),
				conf.getString("jdbc.oracle.work.235.password"));
		runner = new QueryRunner();

	}

	/**
	 * 应用单例模式，返回类本身。
	 * 
	 * @return
	 */

	public static StandardClassUtils getInstance() {
		if (instance == null) {
			instance = new StandardClassUtils();
		}
		return instance;
	}

	/**
	 * 得到国内标准类别所有信息
	 */
	private List<Map<String, Object>> getGNClassAllInfo() {

		List<Map<String, Object>> resultList = null;

		String sql = "select * from st_class st where st.class_layer=3 and st.father_class_code in (select s.class_code from st_class s where s.class_layer=2 and s.father_class_code ='GN' )";

		out.println("sql : " + sql);
		try {

			resultList = runner.query(conn, sql, new MapListHandler());

			// 测试
			// if (resultList != null) // 如果查不到数据，返回值为 null，故需要判断
			// for (Map m : resultList) {
			// out.println(StringUtils.center("信息", 80, '*'));
			//
			// for (Iterator it = m.entrySet().iterator(); it.hasNext();) { //
			// 每条标准的具体信息
			// Map.Entry entry = (Map.Entry) it.next();
			// out.println(entry.getKey() + " = " + entry.getValue());
			// }
			// }

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtils.closeQuietly(conn);
			return resultList;
		}

	}

	/**
	 * 得到国内标准类别英文代号
	 * 
	 * @return
	 */
	public List<String> getGNClassCode() {

		List<Map<String, Object>> list = getGNClassAllInfo();

		// 存放最终结果
		List<String> li = new ArrayList<String>();

		if (list != null) //
			for (Map m : list) {
				for (Iterator it = m.entrySet().iterator(); it.hasNext();) { // 每条标准的具体信息
					Map.Entry entry = (Map.Entry) it.next();

					if (!entry.getKey().toString().equals("CLASS_CODE"))
						continue;
					li.add(entry.getValue().toString());
				}
			}

		return li;
	}

	/**
	 * 得到国外标准分类所有信息
	 */
	private List<Map<String, Object>> getGWClassAllInfo() {

		List<Map<String, Object>> resultList = null;

		String sql = "select * from st_class st where st.class_layer ='2' and st.father_class_code='GW'";

		out.println("sql : " + sql);
		try {

			resultList = runner.query(conn, sql, new MapListHandler());

			// 测试
			// if (resultList != null) // 如果查不到数据，返回值为 null，故需要判断
			// for (Map m : resultList) {
			// out.println(StringUtils.center("信息", 80, '*'));
			//
			// for (Iterator it = m.entrySet().iterator(); it.hasNext();) { //
			// 每条标准的具体信息
			// Map.Entry entry = (Map.Entry) it.next();
			// out.println(entry.getKey() + " = " + entry.getValue());
			// }
			// }

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtils.closeQuietly(conn);
			return resultList;
		}

	}

	/**
	 * 得到国外标准类别名称
	 * 
	 * @return
	 */
	public List<String> getGWClassName() {

		List<Map<String, Object>> list = getGWClassAllInfo();

		// 存放最终结果
		List<String> li = new ArrayList<String>();

		if (list != null) //
			for (Map m : list) {
				for (Iterator it = m.entrySet().iterator(); it.hasNext();) { // 每条标准的具体信息
					Map.Entry entry = (Map.Entry) it.next();

					if (!entry.getKey().toString().equals("CLASS_CODE"))
						continue;
					li.add(entry.getValue().toString());
				}
			}

		return li;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//
		StandardClassUtils sc = new StandardClassUtils();

		int count = 0;

		// 测试
		for (String s : sc.getGWClassName())

			out.println(s);
		out.println(StringUtils.center("类别信息", 80, '*'));

		for (String s : sc.getGNClassCode())
			out.println(s);

	}

}